//
//  TheKeyboardView.m
//  HAODE
//
//  Created by panquanReabam.com on 16/5/19.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "TheKeyboardView.h"
#import "GoodsView.h"
@interface TheKeyboardView()
@property BOOL isContinue;
@property BOOL isUseInEnteringANumber;
@end

@implementation TheKeyboardView
{
NSString*str;
NSString*strs;
NSString*strPay;
}
@synthesize mDelegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _isUseInEnteringANumber = YES;
        _isContinue=NO;
        self.shu=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
        self.shu.text=@"";
        [self addSubview:self.shu];
        self.shu1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
        self.shu1.text=@"";
        [self addSubview:self.shu1];
       
        _payAmount=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-15,ScreenHeight-64-ScreenWidth-60)];
        _payAmount.text=[NSString stringWithFormat:@"%@", @"0.00"];
        _payAmount.textAlignment = NSTextAlignmentRight;
        _payAmount.font=[UIFont systemFontOfSize:PriceFot];
        _payAmount.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
        _payAmount.textColor=[UIColor colorWithRed:225/255.0 green:238/255.0 blue:203/255.0 alpha:1.0];
        [self addSubview:_payAmount];
        
        UILabel*bianView=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-15,0,15,ScreenHeight-64-ScreenWidth-60)];
        bianView.backgroundColor=[UIColor colorWithRed:14/255.0 green:23/255.0 blue:0/255.0 alpha:1.0];
        [self addSubview:bianView];
        
        _wufuhaopayAmount=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth-15,60)];
        _wufuhaopayAmount.text=[NSString stringWithFormat:@"%@", @"0.00"];
        _wufuhaopayAmount.textAlignment = NSTextAlignmentRight;
        _wufuhaopayAmount.textColor=[UIColor clearColor];
        [self addSubview:_wufuhaopayAmount];
        
        UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0,0.45, ScreenWidth,0.35)];
        line.backgroundColor=luseColor;
        [self addSubview:line];
        
        _jiajianhao=[[UILabel alloc]initWithFrame:CGRectMake(0, _payAmount.bottom-40, ScreenWidth-15,40)];
        _jiajianhao.textAlignment = NSTextAlignmentRight;
        _jiajianhao.textColor=luseColor;
        _jiajianhao.font=[UIFont boldSystemFontOfSize:20];
        [self addSubview:_jiajianhao];

        NSArray *array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@".",@"0"];
        for (int i = 0,j = 0; i < array.count; i++) {
            if (i % 3 == 0) {
                j++;
            }
            UIButton *digitalbutton = [UIButton buttonWithType:UIButtonTypeCustom];
            digitalbutton.frame = CGRectMake(ScreenWidth/4*(i%3),_payAmount.bottom-ScreenWidth/4+(ScreenWidth/4*j), ScreenWidth/4, ScreenWidth/4);
            digitalbutton.tag = 301+i;
            [digitalbutton setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];
            [digitalbutton setTitleColor:shenhuiColor forState:UIControlStateNormal];
            digitalbutton.backgroundColor=zuiqianhuiColor;
            [digitalbutton addTarget:self action:@selector(digital:) forControlEvents:UIControlEventTouchUpInside];
            digitalbutton.layer.borderColor = qianhuiColor.CGColor;
            [digitalbutton.layer setBorderWidth:0.55];   //边
            digitalbutton.titleLabel.font=[UIFont systemFontOfSize:24];
            [self addSubview:digitalbutton];
        }
        
        UIButton *empty = [UIButton buttonWithType:UIButtonTypeCustom];
        empty.frame = CGRectMake((ScreenWidth/4*3),_payAmount.bottom-ScreenWidth/4+(ScreenWidth/4*1), ScreenWidth/4, ScreenWidth/4);
        [empty setTitle:@"←" forState:UIControlStateNormal];
        [empty setTitleColor:shenhuiColor forState:UIControlStateNormal];
        empty.backgroundColor=zuiqianhuiColor;
        [empty addTarget:self action:@selector(empty:) forControlEvents:UIControlEventTouchUpInside];
        empty.layer.borderColor = qianhuiColor.CGColor;
        [empty.layer setBorderWidth:0.55];   //边
        empty.titleLabel.font=[UIFont systemFontOfSize:24];
        [self addSubview:empty];
        NSArray *array1 = @[@"+",@"-"];
        for (int i = 0; i < array1.count; i++) {
            
            UIButton *AddAndSubtractBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            AddAndSubtractBtn.frame = CGRectMake((ScreenWidth/4*3),empty.bottom+ScreenWidth/4*i, ScreenWidth/4, ScreenWidth/4);
            AddAndSubtractBtn.tag = 201+i;
            [AddAndSubtractBtn setTitle:[array1 objectAtIndex:i] forState:UIControlStateNormal];
            [AddAndSubtractBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
            AddAndSubtractBtn.backgroundColor=zuiqianhuiColor;
            [AddAndSubtractBtn addTarget:self action:@selector(AddAndSubtract:) forControlEvents:UIControlEventTouchUpInside];
            AddAndSubtractBtn.layer.borderColor = qianhuiColor.CGColor;
            [AddAndSubtractBtn.layer setBorderWidth:0.55];   //边
            AddAndSubtractBtn.titleLabel.font=[UIFont systemFontOfSize:24];
            [self addSubview:AddAndSubtractBtn];
        }
        UIButton *dengyu = [UIButton buttonWithType:UIButtonTypeCustom];
        dengyu.frame = CGRectMake(ScreenWidth/2,_payAmount.bottom-ScreenWidth/4+(ScreenWidth/4*4), ScreenWidth/4*2, ScreenWidth/4);
        [dengyu setTitle:@"=" forState:UIControlStateNormal];
        [dengyu setTitleColor:shenhuiColor forState:UIControlStateNormal];
        dengyu.backgroundColor=zuiqianhuiColor;
        [dengyu addTarget:self action:@selector(dengyu:) forControlEvents:UIControlEventTouchUpInside];
        dengyu.layer.borderColor = qianhuiColor.CGColor;
        [dengyu.layer setBorderWidth:0.55];   //边
        dengyu.titleLabel.font=[UIFont systemFontOfSize:24];
        [self addSubview:dengyu];
        UILabel*line1=[[UILabel alloc]initWithFrame:CGRectMake(0, _payAmount.bottom-0.45, ScreenWidth,0.45)];
        line1.backgroundColor=qianhuiColor;
        [self addSubview:line1];
        
        UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-60-64, ScreenWidth/4, 60)];
        btn.backgroundColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        [btn addTarget:self action:@selector(cashBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        _cashBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-20-64, ScreenWidth/4, 13)];
        [_cashBtn  setTitle:@"现金" forState:UIControlStateNormal];
        [_cashBtn addTarget:self action:@selector(cashBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_cashBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _cashBtn.titleLabel .font=[UIFont systemFontOfSize:13];
        [self addSubview:_cashBtn];
        UIButton*imagebtn=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-60-64, ScreenWidth/4, 40)];
        [imagebtn addTarget:self action:@selector(cashBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:imagebtn];
        UIImageView*imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 45, 45)];
        imageview.image=[UIImage imageNamed:@"xianjins"];
        imageview.center=imagebtn.center;
        [self addSubview:imageview];
        UIButton*shuakabtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4, ScreenHeight-60-64, ScreenWidth/4, 60)];
        shuakabtn.backgroundColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        [shuakabtn addTarget:self action:@selector(creditCardBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:shuakabtn];
        _creditCardBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4, ScreenHeight-20-64, ScreenWidth/4, 13)];
        [_creditCardBtn  setTitle:@"刷卡" forState:UIControlStateNormal];
        [_creditCardBtn addTarget:self action:@selector(creditCardBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_creditCardBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _creditCardBtn.titleLabel .font=[UIFont systemFontOfSize:13];
        [self addSubview:_creditCardBtn];
        UIButton*creditCardBtnimagebtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4, ScreenHeight-60-64, ScreenWidth/4, 40)];
        [creditCardBtnimagebtn addTarget:self action:@selector(creditCardBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:creditCardBtnimagebtn];
        UIImageView*creditCardBtnimagebtnimageview=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/4, 0, 45, 45)];
        creditCardBtnimagebtnimageview.image=[UIImage imageNamed:@"shuakas"];
        creditCardBtnimagebtnimageview.center=creditCardBtnimagebtn.center;
        [self addSubview:creditCardBtnimagebtnimageview];
        
        UIButton*payTreasureBtn1=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*2, ScreenHeight-60-64, ScreenWidth/4, 60)];
        payTreasureBtn1.backgroundColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        payTreasureBtn1.tag=0;
        [payTreasureBtn1 addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:payTreasureBtn1];
        _payTreasureBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*2, ScreenHeight-20-64, ScreenWidth/4, 13)];
        [_payTreasureBtn  setTitle:@"支付宝" forState:UIControlStateNormal];
        _payTreasureBtn.tag=0;
        [_payTreasureBtn addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
        [_payTreasureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _payTreasureBtn.titleLabel .font=[UIFont systemFontOfSize:13];
        [self addSubview:_payTreasureBtn];
        UIButton*zhifubaobtn1imagebtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*2, ScreenHeight-60-64, ScreenWidth/4, 40)];
        [zhifubaobtn1imagebtn addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
        zhifubaobtn1imagebtn.tag=0;
        [self addSubview:zhifubaobtn1imagebtn];
        UIImageView*zhifubaobtnimageview=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/4*2, 0, 45, 45)];
        zhifubaobtnimageview.image=[UIImage imageNamed:@"zhifubaos"];
        zhifubaobtnimageview.center=zhifubaobtn1imagebtn.center;
        [self addSubview:zhifubaobtnimageview];
        UIButton*weChatBtn1=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*3, ScreenHeight-60-64, ScreenWidth/4, 60)];
        weChatBtn1.backgroundColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        weChatBtn1.tag=1;
        [weChatBtn1 addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:weChatBtn1];
        _weChatBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*3, ScreenHeight-20-64, ScreenWidth/4, 13)];
        [_weChatBtn  setTitle:@"微信" forState:UIControlStateNormal];
        _weChatBtn.tag=1;
        [_weChatBtn addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
        [_weChatBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _weChatBtn.titleLabel .font=[UIFont systemFontOfSize:13];
        [self addSubview:_weChatBtn];
        UIButton*weChatBtnimagebtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*3, ScreenHeight-60-64, ScreenWidth/4, 40)];
        [weChatBtnimagebtn addTarget:self action:@selector(payTreasure:) forControlEvents:UIControlEventTouchUpInside];
        weChatBtnimagebtn.tag=1;
        [self addSubview:weChatBtnimagebtn];
        UIImageView*weChatBtnimagebtnimageview=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/4*3, 0, 45, 45)];
        weChatBtnimagebtnimageview.image=[UIImage imageNamed:@"weixins"];
        weChatBtnimagebtnimageview.center=weChatBtnimagebtn.center;
        [self addSubview:weChatBtnimagebtnimageview];

    }
    return self;
}

- (void)digital:(UIButton *)sender
{
    if (_isContinue) {
        NSString *digit = sender.currentTitle;
        if ([sender.titleLabel.text isEqualToString: @"." ]) {
            digit=@"";
        }
       [self digitalPayAmountStr:digit];
        
    }else{
        if ([sender.titleLabel.text isEqualToString: @"." ]) {
            _isContinue=YES;
        }
        NSString *digit = sender.currentTitle;
        [self digitalPayAmountStr:digit];
    }
    
}


- (void)AddAndSubtract:(UIButton *)sender {
    if ([self.shu1 .text length]>0) {
        double d1;
        double d;
        if ([self.shu.text isEqualToString:@""]){
            self.shu.text =_wufuhaopayAmount.text;
        } else {
        }
        d=[self.shu.text doubleValue];
        d1=[self.shu1.text doubleValue];
        if ([str isEqualToString:@"+"]) {
            NSString*payAmountStr=[NSString stringWithFormat:@"%.2f",d+d1];
            [self payAmount:payAmountStr];
        }else{
            NSString*payAmountStr=[NSString stringWithFormat:@"%.2f", d-d1];
            [self payAmount:payAmountStr];
        }
        _isUseInEnteringANumber = YES;
        _isContinue=NO;
        self.shu.text=@"";
        self.shu1.text = @"";
        if (sender.tag==201) {
            str=@"+";
            NSString *digit = sender.currentTitle;
            _jiajianhao.text = [_jiajianhao.text stringByAppendingString:digit];
            _isUseInEnteringANumber = NO;
            _isContinue=NO;
            
        }else{
            str=@"-";
            NSString *digit = sender.currentTitle;
            _jiajianhao.text = [_jiajianhao.text stringByAppendingString:digit];
            _isUseInEnteringANumber = NO;
            _isContinue=NO;
        }
    }else
        if ([_jiajianhao.text isEqualToString:@""]) {
        }else{
            
            if ([str length]>0) {
            }else{
                if (sender.tag==201) {
                    str=@"+";
                    NSString *digit = sender.currentTitle;
                    
                    _jiajianhao.text = [_jiajianhao.text stringByAppendingString:digit];
                    _isUseInEnteringANumber = NO;
                    _isContinue=NO;
                    
                }else{
                    str=@"-";
                    NSString *digit = sender.currentTitle;
                    _jiajianhao.text = [_jiajianhao.text stringByAppendingString:digit];
                    _isUseInEnteringANumber = NO;
                    _isContinue=NO;
                }}
        }
}


- (void)dengyu:(UIButton *)sender {
    double d1;
    double d;
    if ([self.shu.text isEqualToString:@""]){
        self.shu.text =_wufuhaopayAmount.text;
    } else {
    }
    d=[self.shu.text doubleValue];
    d1=[self.shu1.text doubleValue];
    if ([str isEqualToString:@"+"]) {
        NSString*payAmountStr=[NSString stringWithFormat:@"%.2f", d+d1];
        [self payAmount:payAmountStr];
    }else{
        NSString*payAmountStr=[NSString stringWithFormat:@"%.2f", d-d1];
        [self payAmount:payAmountStr];
    }
    _isUseInEnteringANumber = YES;
    _isContinue=NO;
    self.shu.text=@"";
    self.shu1.text = @"";
    str=@"";
}

//算法加减
-(void)digitalPayAmountStr:(NSString*)digit{
    if (_isUseInEnteringANumber){
        _jiajianhao.text = [self.shu.text stringByAppendingString:digit];
        self.shu.text = [self.shu.text stringByAppendingString:digit];
        NSString*GoldPrice=[NSString stringWithFormat:@"%.2f", self.shu.text.doubleValue];
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setPositiveFormat:@"###,##0.00;"];
        NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
        _payAmount.text=[NSString stringWithFormat:@"¥ %.2f", self.shu.text.doubleValue];
        _payAmount.textAlignment = NSTextAlignmentRight;
        _payAmount.textColor=[UIColor colorWithRed:225/255.0 green:238/255.0 blue:203/255.0 alpha:1.0];
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@" ,formattedNumberString]];
        
        [AttributedStr addAttribute:NSFontAttributeName
         
                              value:[UIFont systemFontOfSize:25.0]
         
                              range:NSMakeRange(0, 1)];
        
        [AttributedStr addAttribute:NSForegroundColorAttributeName
         
                              value:[UIColor colorWithRed:225/255.0 green:238/255.0 blue:203/255.0 alpha:1.0]
         
                              range:NSMakeRange(0, 1)];
        _payAmount.attributedText = AttributedStr;
        _wufuhaopayAmount.text=self.shu.text;
    } else {
        _jiajianhao.text = [_jiajianhao.text stringByAppendingString:digit];
        self.shu1.text = [self.shu1.text stringByAppendingString:digit];
        
    }
}
- (void)payAmount:(NSString *)payAmountStr {
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:payAmountStr.floatValue]];
    _payAmount.text=[NSString stringWithFormat:@"¥ %@", formattedNumberString];
    _payAmount.textAlignment = NSTextAlignmentRight;
    _payAmount.textColor=[UIColor colorWithRed:225/255.0 green:238/255.0 blue:203/255.0 alpha:1.0];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@" ,formattedNumberString]];
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:25.0]
     
                          range:NSMakeRange(0, 1)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor colorWithRed:225/255.0 green:238/255.0 blue:203/255.0 alpha:1.0]
     
                          range:NSMakeRange(0, 1)];
    _payAmount.attributedText = AttributedStr;
    _wufuhaopayAmount.text=payAmountStr;
    _jiajianhao.text=payAmountStr;
    
}
- (void)empty:(UIButton *)sender {
    _payAmount.text = @"0.00";
    self.shu.text = @"";
    self.shu1.text = @"";
    _jiajianhao.text = @"";
    _isUseInEnteringANumber = YES;
    _isContinue=NO;
    str=@"";
}


-(void)creditCardBtn:(UIButton*)sender{
    
    [_weChatBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_payTreasureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_creditCardBtn setTitleColor:shenluse forState:UIControlStateNormal];
    [_cashBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [mDelegate creditCardBtn:@""];
}
-(void)payTreasure:(UIButton*)sender{
    if (sender.tag==0) {
        [_payTreasureBtn setTitleColor:shenluse forState:UIControlStateNormal];
        [_weChatBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        strPay=@"AliQrPay";
    }else{
    
        [_payTreasureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_weChatBtn setTitleColor:shenluse forState:UIControlStateNormal];
         strPay=@"WxScanPay";
    }
   
    [_creditCardBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_cashBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [mDelegate payTreasure:strPay];
}
-(void)cashBtn:(UIButton*)sender{
    
    [_weChatBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_payTreasureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_creditCardBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_cashBtn setTitleColor:shenluse forState:UIControlStateNormal];
    [mDelegate cashBtn:@""];
}


@end
